﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Plugin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace Vant.WpfControls
{
    public class ColorHelper
    {
        static ColorHelper()
        {
            var converter = new BrushConverter();
            Success = converter.ConvertFrom("#67C23A") as Brush;
            Info = converter.ConvertFrom("#909399") as Brush;
            Warning = converter.ConvertFrom("#E6A23C") as Brush;
            Error = converter.ConvertFrom("#F56C6C") as Brush;
            Text = converter.ConvertFrom("#323233") as Brush;
            DarkText = converter.ConvertFrom("#303133") as Brush;
            BorderColorLighter = converter.ConvertFrom("#EBEEF5") as Brush;
            BorderColorLightest = converter.ConvertFrom("#c0c4cc") as Brush;
        }

        public static Brush Success { get; set; }
        public static Brush Info { get; set; }
        public static Brush Warning { get; set; }

        internal static Brush FromStr(string str)
        {
            return new BrushConverter().ConvertFrom(str) as Brush;
        }

        public static Brush Error { get; set; }
        public static Brush Text { get; set; }
        public static Brush DarkText { get; set; }
        public static Brush BorderColorLighter { get; set; }
        public static Brush BorderColorLightest { get; set; }
    }
    public class IconHelper
    {
        public static object GetIcon(ImageValue image, IDrawingHelper drawingHelper)
        {
            if (string.IsNullOrEmpty(image?.Name))
            {
                return null;
            }
            var path = drawingHelper.GetForguncyImageFullFileName(image.Name, image.BuiltIn);
            return drawingHelper.GetForguncyImageControl(path, image.Color);
        }

        public static object GetBuiltInIcon(string name, IDrawingHelper drawingHelper, string color = "#969799")
        {
            var assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
            var folder = System.IO.Path.GetDirectoryName(assemblyLocation);
            var iconFile = System.IO.Path.Combine(folder, "Resources", "images", name + ".svg");

            return drawingHelper.GetSvgControl(iconFile, color);
        }
    }
    public class StyleHelper
    {
        public static CellTypeViewModelStyle CreateStyleViewModel(IStyleInfo style, IDrawingHelper drawingHelper)
        {
            var result = new CellTypeViewModelStyle();

            if(style.FontSize > 0)
            {
                result.FontSize = style.FontSize;
            }
            if (!string.IsNullOrEmpty(style.FontFamily))
            {
                result.FontFamily = drawingHelper.GetFontFamily(style.FontFamily);
            }
            result.TextWrapping = style.WordWrap ? TextWrapping.Wrap : TextWrapping.NoWrap;
            result.TextDecoration = new TextDecorationCollection();
            if (style.Underline == true)
            {
                result.TextDecoration.Add(TextDecorations.Underline);
            }
            if (style.Strikethrough == true)
            {
                result.TextDecoration.Add(TextDecorations.Strikethrough);
            }
            if(style.ForegroundStr != null)
            {
                result.Foreground = drawingHelper.GetBrush(style.ForegroundStr);
            }
            if(style.FontWeight?.ToLower() == "bold")
            {
                result.FontWeight = FontWeights.Bold;
            }
            if (style.FontStyle?.ToLower() == "italic")
            {
                result.FontStyle = FontStyles.Italic;
            }

            return result;
        }
    }

    public class CellTypeViewModelStyle : ModelBase
    {

        private Brush _foreground;
        public Brush Foreground
        {
            get
            {
                return this._foreground;
            }
            set
            {
                if (_foreground != value)
                {
                    this._foreground = value;
                    this.OnPropertyChanged();
                }
            }
        }

        private double _fontSize;
        public double FontSize
        {
            get
            {
                return this._fontSize;
            }
            set
            {
                if (_fontSize != value)
                {
                    this._fontSize = value;
                    this.OnPropertyChanged();
                }
            }
        }

        private FontFamily _fontFamily;
        public FontFamily FontFamily
        {
            get
            {
                return this._fontFamily;
            }
            set
            {
                if (_fontFamily != value)
                {
                    this._fontFamily = value;
                    this.OnPropertyChanged();
                }
            }
        }

        private FontStyle _fontStyle;
        public FontStyle FontStyle
        {
            get
            {
                return this._fontStyle;
            }
            set
            {
                if (_fontStyle != value)
                {
                    this._fontStyle = value;
                    this.OnPropertyChanged();
                }
            }
        }

        private FontWeight _fontWeight;
        public FontWeight FontWeight
        {
            get
            {
                return this._fontWeight;
            }
            set
            {
                if (_fontWeight != value)
                {
                    this._fontWeight = value;
                    this.OnPropertyChanged();
                }
            }
        }

        private TextDecorationCollection _textDecoration;
        public TextDecorationCollection TextDecoration
        {
            get
            {
                return this._textDecoration;
            }
            set
            {
                if (_textDecoration != value)
                {
                    this._textDecoration = value;
                    this.OnPropertyChanged();
                }
            }
        }

        public TextWrapping TextWrapping { get; set; }
    }
}
